package com.djhu.es.core;
/**
 * Created by zw on 2017/12/28.
 */

import com.djhu.es.core.request.*;
import net.minidev.json.JSONArray;
import net.sf.json.JSONObject;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetSocketAddress;
import java.util.List;
import java.util.Map;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * <p>
 * ---------------------------------
 */
public class SingleClientSource implements ClientSource{
    private String host;
    private int port;
    private Settings settings;

    private Client client;
    public SingleClientSource(String host, int port) {
        this.host = host;
        this.port = port;
        init();
    }

    public SingleClientSource(String host, int port, Settings settings) {
        this.host = host;
        this.port = port;
        this.settings = settings;
        init();
    }

    public void init(){
        if (settings==null){
            settings = Settings.builder()
                    .put("client.transport.sniff", false).put("client.transport.ignore_cluster_name", true).build();
        }
        InetSocketTransportAddress transportAddress = new InetSocketTransportAddress(new InetSocketAddress(this.host, this.port));
        client = new PreBuiltTransportClient(settings).addTransportAddress(transportAddress);
    }


    @Override
    public Client getClient() {
        if (client != null){
            return client;
        }else {
            init();
            return client;
        }
    }

    @Override
    public void destory() throws Exception {
        client.close();
    }

    @Override
    public void returnResource(Client client) throws Exception {
//       if (client != null){
//           client.close();
//       }
    }

    public static class Student{
        private String patient_meta_list;
        private String received_time;
        private String documentuniqueid;

        public String getPatient_meta_list() {
            return patient_meta_list;
        }

        public void setPatient_meta_list(String patient_meta_list) {
            this.patient_meta_list = patient_meta_list;
        }

        public String getReceived_time() {
            return received_time;
        }

        public void setReceived_time(String received_time) {
            this.received_time = received_time;
        }

        public String getDocumentuniqueid() {
            return documentuniqueid;
        }

        public void setDocumentuniqueid(String documentuniqueid) {
            this.documentuniqueid = documentuniqueid;
        }
    }

    public static void main(String[] args) throws Exception {
        SingleClientSource singleClientSource = new SingleClientSource("192.168.130.192",9300);
        ElasticsearchTemplate elasticsearchTemplate = new ElasticsearchTemplate(singleClientSource);
        TermSearchRequest searchRequest = new TermSearchRequest();
        searchRequest.setIndex("datatest");
        searchRequest.setType("datatest");
        searchRequest.setTimeout(1000);
        searchRequest.setField("version.keyword");
        searchRequest.setValue("2.0.0.0");
//        List<Student> student = elasticsearchTemplate.queryforlist(searchRequest,Student.class);
//        List<Map<String, Object>> maps = elasticsearchTemplate.queryforList(searchRequest);
//        System.out.println(JSONArray.toJSONString(student));
//        singleClientSource.destory();


        JsonAddRequest addRequest=new JsonAddRequest();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name","test");
        jsonObject.put("date","2018-04-08 12:20:20");
        addRequest.setJsonObject(jsonObject);
        addRequest.setIndex("datatest1");
        addRequest.setType("datatest1");
        elasticsearchTemplate.add(addRequest);


    }

}